package com.quanyan.stadium.service.gameRules.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbGameRound;
import com.quanyan.stadium.entity.TbGameSchedule;
import com.quanyan.stadium.entity.TbGameScheduleAddendum;
import com.quanyan.stadium.entity.vo.VDefinedGameSchedule;
import com.quanyan.stadium.enums.GameSystemUnsetDoubleConfigEnum;
import com.quanyan.stadium.mapper.TbGameScheduleAddendumMapper;
import com.quanyan.stadium.service.gameRules.GameSystemServiceEight;
import com.quanyan.stadium.service.gameRules.GameSystemServiceUnset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("gameSystemServiceUnset")
public class GameSystemServiceUnsetImpl implements GameSystemServiceUnset {

    @Autowired
    private GameSystemServiceEight gameSystemServiceEight;

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private TbGameScheduleAddendumMapper tbGameScheduleAddendumMapper;

    private static final Logger logger = LoggerFactory.getLogger(GameSystemServiceUnsetImpl.class);

    @Override
    public void generateUnsetGameSystem(List<Integer> teamIds, List<TbGameRound> tbGameRounds) {
        if (null!=tbGameRounds && tbGameRounds.size() != 1) {
            return;
        }
        TbGameRound tbGameRound = tbGameRounds.get(0);
        try{
            if (teamIds.size() == 5) {
                this.generateFiveGameSystem(teamIds, tbGameRound);
            }else if (teamIds.size() == 6){
                this.generateSixGameSystem(teamIds, tbGameRound);
            }else if (teamIds.size() == 7){
                this.generateSevenGameSystem(teamIds, tbGameRound);
            }else if (teamIds.size() == 8){
                this.generateEightGameSystem(teamIds, tbGameRound);
            }
        }catch (Exception e) {
            logger.error("百灵鸟5-8人制比赛的"+teamIds.size()+"人制赛制生成出错,原因:{}", e);
        }
    }

    @Override
    public void generateFiveGameSystem(List<Integer> teamIds, TbGameRound tbGameRound) {
        int scheduleNum = GameSystemUnsetDoubleConfigEnum.getRoundNumByTeamNum(teamIds.size());
        for (int i=0;i<scheduleNum;i++) {
            TbGameSchedule schedule = new TbGameSchedule();
            TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();
            schedule.setGameRoundId(tbGameRound.getId());
            switch (i) {
                case 0:
                    TbGameSchedule scheduleTemp11 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp11.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(1));
                    addendum2.setGameScheduleId(scheduleTemp11.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp11.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(3));
                    addendum4.setGameScheduleId(scheduleTemp11.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(4));
                    break;
                case 1:
                    TbGameSchedule scheduleTemp21 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp21.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp21.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp21.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp21.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(4));

                    break;
                case 2:
                    TbGameSchedule scheduleTemp31 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp31.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp31.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp31.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp31.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));

                    break;
                case 3:
                    TbGameSchedule scheduleTemp41 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp41.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp41.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(4));

                    addendum3.setGameScheduleId(scheduleTemp41.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(2));
                    addendum4.setGameScheduleId(scheduleTemp41.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));

                    break;
                case 4:
                    TbGameSchedule scheduleTemp51 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp51.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp51.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp51.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp51.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(4));
                    break;
                case 5:
                    TbGameSchedule scheduleTemp61 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp61.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp61.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(1));

                    addendum3.setGameScheduleId(scheduleTemp61.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(2));
                    addendum4.setGameScheduleId(scheduleTemp61.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));
                    break;

                default:
                    System.out.print("fuck");
                    break;
            }
            tbGameScheduleAddendumMapper.insertSelective(addendum1);
            tbGameScheduleAddendumMapper.insertSelective(addendum2);
            tbGameScheduleAddendumMapper.insertSelective(addendum3);
            tbGameScheduleAddendumMapper.insertSelective(addendum4);

        }

    }

    @Override
    public void generateSixGameSystem(List<Integer> teamIds, TbGameRound tbGameRound) {
        int scheduleNum = GameSystemUnsetDoubleConfigEnum.getRoundNumByTeamNum(teamIds.size());
        for (int i = 0; i < scheduleNum; i++) {
            TbGameSchedule schedule = new TbGameSchedule();
            TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();
            schedule.setGameRoundId(tbGameRound.getId());
            switch (i) {
                case 0:
                    TbGameSchedule scheduleTemp11 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp11.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(2));
                    addendum2.setGameScheduleId(scheduleTemp11.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp11.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp11.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));
                    break;
                case 1:
                    TbGameSchedule scheduleTemp21 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp21.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp21.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(4));

                    addendum3.setGameScheduleId(scheduleTemp21.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp21.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));

                    break;
                case 2:
                    TbGameSchedule scheduleTemp31 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp31.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp31.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp31.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp31.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));

                    break;
                case 3:
                    TbGameSchedule scheduleTemp41 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp41.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(2));
                    addendum2.setGameScheduleId(scheduleTemp41.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(4));

                    addendum3.setGameScheduleId(scheduleTemp41.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(3));
                    addendum4.setGameScheduleId(scheduleTemp41.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));

                    break;
                case 4:
                    TbGameSchedule scheduleTemp51 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp51.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp51.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(5));

                    addendum3.setGameScheduleId(scheduleTemp51.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp51.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(4));
                    break;
                case 5:
                    TbGameSchedule scheduleTemp61 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp61.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp61.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(1));

                    addendum3.setGameScheduleId(scheduleTemp61.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(2));
                    addendum4.setGameScheduleId(scheduleTemp61.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));
                    break;

                default:
                    System.out.print("fuck");
                    break;
            }
            tbGameScheduleAddendumMapper.insertSelective(addendum1);
            tbGameScheduleAddendumMapper.insertSelective(addendum2);
            tbGameScheduleAddendumMapper.insertSelective(addendum3);
            tbGameScheduleAddendumMapper.insertSelective(addendum4);
        }

    }

    @Override
    public void generateSevenGameSystem(List<Integer> teamIds, TbGameRound tbGameRound) {
        int scheduleNum = GameSystemUnsetDoubleConfigEnum.getRoundNumByTeamNum(teamIds.size());
        for (int i = 0; i < scheduleNum; i++) {
            TbGameSchedule schedule = new TbGameSchedule();
            TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();
            schedule.setGameRoundId(tbGameRound.getId());
            switch (i) {
                case 0:
                    TbGameSchedule scheduleTemp11 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp11.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp11.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp11.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp11.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));
                    break;
                case 1:
                    TbGameSchedule scheduleTemp21 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp21.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(1));
                    addendum2.setGameScheduleId(scheduleTemp21.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp21.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp21.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));

                    break;
                case 2:
                    TbGameSchedule scheduleTemp31 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp31.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp31.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp31.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(5));
                    addendum4.setGameScheduleId(scheduleTemp31.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));

                    break;
                case 3:
                    TbGameSchedule scheduleTemp41 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp41.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(1));
                    addendum2.setGameScheduleId(scheduleTemp41.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp41.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(3));
                    addendum4.setGameScheduleId(scheduleTemp41.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(4));

                    break;
                case 4:
                    TbGameSchedule scheduleTemp51 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp51.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp51.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(5));

                    addendum3.setGameScheduleId(scheduleTemp51.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp51.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));
                    break;
                case 5:
                    TbGameSchedule scheduleTemp61 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp61.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(2));
                    addendum2.setGameScheduleId(scheduleTemp61.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp61.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp61.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));
                    break;
                case 6:
                    TbGameSchedule scheduleTemp71 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp71.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(2));
                    addendum2.setGameScheduleId(scheduleTemp71.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(4));

                    addendum3.setGameScheduleId(scheduleTemp71.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(5));
                    addendum4.setGameScheduleId(scheduleTemp71.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));
                    break;
                case 7:
                    TbGameSchedule scheduleTemp81 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp81.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp81.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(1));

                    addendum3.setGameScheduleId(scheduleTemp81.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(2));
                    addendum4.setGameScheduleId(scheduleTemp81.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));
                    break;

                default:
                    System.out.print("fuck");
                    break;
            }
            tbGameScheduleAddendumMapper.insertSelective(addendum1);
            tbGameScheduleAddendumMapper.insertSelective(addendum2);
            tbGameScheduleAddendumMapper.insertSelective(addendum3);
            tbGameScheduleAddendumMapper.insertSelective(addendum4);
        }
    }

    @Override
    public void generateEightGameSystem(List<Integer> teamIds, TbGameRound tbGameRound) {
        int scheduleNum = GameSystemUnsetDoubleConfigEnum.getRoundNumByTeamNum(teamIds.size());
        for (int i = 0; i < scheduleNum; i++) {
            TbGameSchedule schedule = new TbGameSchedule();
            TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
            TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();
            schedule.setGameRoundId(tbGameRound.getId());
            switch (i) {
                case 0:
                    TbGameSchedule scheduleTemp11 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp11.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp11.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp11.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp11.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));
                    break;
                case 1:
                    TbGameSchedule scheduleTemp21 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp21.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(1));
                    addendum2.setGameScheduleId(scheduleTemp21.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp21.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(5));
                    addendum4.setGameScheduleId(scheduleTemp21.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(7));

                    break;
                case 2:
                    TbGameSchedule scheduleTemp31 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp31.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp31.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp31.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(5));
                    addendum4.setGameScheduleId(scheduleTemp31.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(6));

                    break;
                case 3:
                    TbGameSchedule scheduleTemp41 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp41.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(1));
                    addendum2.setGameScheduleId(scheduleTemp41.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(2));

                    addendum3.setGameScheduleId(scheduleTemp41.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp41.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(7));

                    break;
                case 4:
                    TbGameSchedule scheduleTemp51 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp51.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp51.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(6));

                    addendum3.setGameScheduleId(scheduleTemp51.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(1));
                    addendum4.setGameScheduleId(scheduleTemp51.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(7));
                    break;
                case 5:
                    TbGameSchedule scheduleTemp61 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp61.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(2));
                    addendum2.setGameScheduleId(scheduleTemp61.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(3));

                    addendum3.setGameScheduleId(scheduleTemp61.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(4));
                    addendum4.setGameScheduleId(scheduleTemp61.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(5));
                    break;
                case 6:
                    TbGameSchedule scheduleTemp71 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp71.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(4));
                    addendum2.setGameScheduleId(scheduleTemp71.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(5));

                    addendum3.setGameScheduleId(scheduleTemp71.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(6));
                    addendum4.setGameScheduleId(scheduleTemp71.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(7));
                    break;
                case 7:
                    TbGameSchedule scheduleTemp81 = bizGameSystemDao.insertTbGameSchedule(schedule);
                    addendum1.setGameScheduleId(scheduleTemp81.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(teamIds.get(0));
                    addendum2.setGameScheduleId(scheduleTemp81.getId());
                    addendum2.setType((byte) 0);
                    addendum2.setEventTeamId(teamIds.get(1));

                    addendum3.setGameScheduleId(scheduleTemp81.getId());
                    addendum3.setType((byte) 1);
                    addendum3.setEventTeamId(teamIds.get(2));
                    addendum4.setGameScheduleId(scheduleTemp81.getId());
                    addendum4.setType((byte) 1);
                    addendum4.setEventTeamId(teamIds.get(3));
                    break;

                default:
                    System.out.print("fuck");
                    break;
            }
            tbGameScheduleAddendumMapper.insertSelective(addendum1);
            tbGameScheduleAddendumMapper.insertSelective(addendum2);
            tbGameScheduleAddendumMapper.insertSelective(addendum3);
            tbGameScheduleAddendumMapper.insertSelective(addendum4);
        }
    }


    public void initUnSetGameRound(List<Integer> newTeamIds, Integer curGameRoundId, Integer preGameRoundId) {
        List<VDefinedGameSchedule> currentGameSchedules = null;
        if (preGameRoundId>0){
            currentGameSchedules = bizGameSystemDao.selectVDefinedGameScheduleByGameRoundId(preGameRoundId, null, null);
        }
        TbGameRound curGameRound = bizGameSystemDao.selectTbGameRound(curGameRoundId);
        if (null!=currentGameSchedules && currentGameSchedules.size()>0) {
            Gson gson = new Gson();
            newTeamIds = gson.fromJson(currentGameSchedules.get(0).getTeamListIndex(), new TypeToken<List<Integer>>() {
            }.getType());
            Map<Integer, Set<VDefinedGameSchedule>> mapSchedules = new HashMap<>();
            Set<VDefinedGameSchedule> setSchedule = new HashSet<>();
            for (VDefinedGameSchedule vDefinedGameSchedule : currentGameSchedules) {
                setSchedule = mapSchedules.get(vDefinedGameSchedule.getGameScheduleId());
                if (null == setSchedule || setSchedule.size() < 1) {
                    setSchedule = new HashSet<>();
                }
                setSchedule.add(vDefinedGameSchedule);
                mapSchedules.put(vDefinedGameSchedule.getGameScheduleId(), setSchedule);
            }

            for (Map.Entry<Integer, Set<VDefinedGameSchedule>> entry : mapSchedules.entrySet()) {
                List<VDefinedGameSchedule> listTemp = new ArrayList<>(entry.getValue());

                VDefinedGameSchedule vDefinedGameSchedule = listTemp.get(0);
                List<Integer> locationIndex = gson.fromJson(vDefinedGameSchedule.getTeamMatchIndex(), new TypeToken<List<Integer>>() {
                }.getType());
                if (vDefinedGameSchedule.getIsWin().intValue() == GameConstants.SCHEDULE_LOSE.intValue()) {  //主场输
                    newTeamIds = this.unsetChangeLocation(newTeamIds, locationIndex);
                }
            }
        }
        try{
            if (newTeamIds.size() == 5) {
                this.unSetTeamNum5(newTeamIds, curGameRound);
            }else if (newTeamIds.size() == 6){
                this.unSetTeamNum6(newTeamIds, curGameRound);
            }else if (newTeamIds.size() == 7){
                this.unSetTeamNum7(newTeamIds, curGameRound);
            }else if (newTeamIds.size() == 8){
                this.unSetTeamNum8(newTeamIds, curGameRound);
            }
        }catch (Exception e) {
            logger.error("百灵鸟5-8人制比赛的"+newTeamIds.size()+"人制赛制生成出错,原因:{}", e);
        }
    }

    private void unSetTeamNum5(List<Integer> teamIds, TbGameRound curGameRound) {
        List<Integer> first = new ArrayList<>();
        int roundStatus = curGameRound.getRoundStatus();
        switch (roundStatus) {
            case 1:
                first.add(1);
                first.add(2);
                first.add(3);
                first.add(4);
                break;
            case 2:
                first.add(0);
                first.add(3);
                first.add(1);
                first.add(4);
                break;
            case 3:
                first.add(0);
                first.add(2);
                first.add(1);
                first.add(3);
                break;
            case 4:
                first.add(0);
                first.add(4);
                first.add(2);
                first.add(3);
                break;
            case 5:
                first.add(0);
                first.add(2);
                first.add(1);
                first.add(4);
                break;
            case 6:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                break;
            default:
                logger.debug("轮次存在异常");
                break;
        }
        this.loadGameScheduleAddendum5To7(curGameRound, first, teamIds);
    }

    private void unSetTeamNum6(List<Integer> teamIds, TbGameRound curGameRound) {
        List<Integer> first = new ArrayList<>();
        int roundStatus = curGameRound.getRoundStatus();
        switch (roundStatus) {
            case 1:
                first.add(2);
                first.add(3);
                first.add(4);
                first.add(5);
                break;
            case 2:
                first.add(0);
                first.add(4);
                first.add(1);
                first.add(5);
                break;
            case 3:
                first.add(0);
                first.add(2);
                first.add(1);
                first.add(3);
                break;
            case 4:
                first.add(2);
                first.add(4);
                first.add(3);
                first.add(5);
                break;
            case 5:
                first.add(0);
                first.add(5);
                first.add(1);
                first.add(4);
                break;
            case 6:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                break;
            default:
                logger.debug("轮次存在异常");
                break;
        }
        this.loadGameScheduleAddendum5To7(curGameRound, first, teamIds);
    }

    private void unSetTeamNum7(List<Integer> teamIds, TbGameRound curGameRound) {
        List<Integer> first = new ArrayList<>();
        int roundStatus = curGameRound.getRoundStatus();
        switch (roundStatus) {
            case 1:
                first.add(0);
                first.add(2);
                first.add(4);
                first.add(6);
                break;
            case 2:
                first.add(1);
                first.add(3);
                first.add(4);
                first.add(5);
                break;
            case 3:
                first.add(0);
                first.add(3);
                first.add(5);
                first.add(6);
                break;
            case 4:
                first.add(1);
                first.add(2);
                first.add(3);
                first.add(4);
                break;
            case 5:
                first.add(0);
                first.add(5);
                first.add(1);
                first.add(6);
                break;
            case 6:
                first.add(2);
                first.add(3);
                first.add(4);
                first.add(5);
            case 7:
                first.add(2);
                first.add(4);
                first.add(5);
                first.add(6);
            case 8:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                break;
            default:
                logger.debug("轮次存在异常");
                break;
        }
        this.loadGameScheduleAddendum5To7(curGameRound, first, teamIds);
    }

    private void unSetTeamNum8(List<Integer> teamIds, TbGameRound curGameRound) {
        List<Integer> first = new ArrayList<>();
        int roundStatus = curGameRound.getRoundStatus();
        switch (roundStatus) {
            case 1:
                first.add(0);
                first.add(2);
                first.add(4);
                first.add(6);
                break;
            case 2:
                first.add(1);
                first.add(3);
                first.add(5);
                first.add(7);
                break;
            case 3:
                first.add(0);
                first.add(3);
                first.add(5);
                first.add(6);
                break;
            case 4:
                first.add(1);
                first.add(2);
                first.add(4);
                first.add(7);
                break;
            case 5:
                first.add(0);
                first.add(6);
                first.add(1);
                first.add(7);
                break;
            case 6:
                first.add(2);
                first.add(3);
                first.add(4);
                first.add(5);
            case 7:
                first.add(4);
                first.add(5);
                first.add(6);
                first.add(7);
            case 8:
                first.add(0);
                first.add(1);
                first.add(2);
                first.add(3);
                break;
            default:
                logger.debug("轮次存在异常");
                break;
        }
        this.loadGameScheduleAddendum5To7(curGameRound, first, teamIds);
    }



    private void loadGameScheduleAddendum5To7(TbGameRound curGameRound,  List<Integer> first,  List<Integer> teamIds){
        Gson gson = new Gson();
        TbGameSchedule schedule = new TbGameSchedule();
        TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum3 = new TbGameScheduleAddendum();
        TbGameScheduleAddendum addendum4 = new TbGameScheduleAddendum();


        schedule.setGameRoundId(curGameRound.getId());
        schedule.setTeamListIndex(gson.toJson(teamIds));
        schedule.setTeamMatchIndex(gson.toJson(first));
        TbGameSchedule scheduleTemp = bizGameSystemDao.insertTbGameSchedule(schedule);


        addendum1.setGameScheduleId(scheduleTemp.getId());
        addendum1.setType((byte) 0);
        addendum1.setEventTeamId(teamIds.get(first.get(0)));
        addendum2.setGameScheduleId(scheduleTemp.getId());
        addendum2.setType((byte) 0);
        addendum2.setEventTeamId(teamIds.get(first.get(1)));
        addendum3.setGameScheduleId(scheduleTemp.getId());
        addendum3.setType((byte) 1);
        addendum3.setEventTeamId(teamIds.get(first.get(2)));
        addendum4.setGameScheduleId(scheduleTemp.getId());
        addendum4.setType((byte) 1);
        addendum4.setEventTeamId(teamIds.get(first.get(3)));


        tbGameScheduleAddendumMapper.insertSelective(addendum1);
        tbGameScheduleAddendumMapper.insertSelective(addendum2);
        tbGameScheduleAddendumMapper.insertSelective(addendum3);
        tbGameScheduleAddendumMapper.insertSelective(addendum4);
    }

    /**
     *  签位置交换
     *
     *
     * locationIndex里面存放上一轮的坐标值(默认从0开始)
     *
     * */
    @Override
    public List<Integer> unsetChangeLocation(List<Integer> teamIds,  List<Integer> locationIndex){
        // swap the elements
        Collections.swap(teamIds, locationIndex.get(0), locationIndex.get(2));
        Collections.swap(teamIds, locationIndex.get(1), locationIndex.get(3));
        return teamIds;
    }
}
